<?php

namespace App\Http\Controllers\Admin;

use App\Models\Participant;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;

// 活动的参与者
class ParticipantController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        // 构建图片需要的数据（最近 7 天）
        $chartData = [
            // ['date' => '2011 Q1', 'count' => 2666],
            // ['date' => '2011 Q2', 'count' => 2778],
            // ['date' => '2011 Q3', 'count' => 4912],
            // ['date' => '2011 Q4', 'count' => 3767],
            // ['date' => '2012 Q1', 'count' => 6810],
            // ['date' => '2012 Q2', 'count' => 5670],
            // ['date' => '2012 Q3', 'count' => 4820],
            // ['date' => '2012 Q4', 'count' => 15073],
            // ['date' => '2013 Q1', 'count' => 10687],
            // ['date' => '2013 Q2', 'count' => 8432]
        ];

        $str = '';
        for ($i = 1; $i <= 14; $i++) {
            $str .= "UNION ALL 
SELECT DATE_SUB(CURDATE(),INTERVAL {$i} DAY) AS date, COUNT(id) AS count FROM thearte_participants 
WHERE updated_at BETWEEN DATE_FORMAT(DATE_SUB(CURDATE(),INTERVAL {$i} DAY), '%Y-%m-%d 00:00:00') AND DATE_FORMAT(DATE_SUB(CURDATE(),INTERVAL {$i} DAY), '%Y-%m-%d 23:59:59') ";
        }
        
        $models = DB::select("
SELECT DATE_SUB(CURDATE(),INTERVAL 0 DAY) AS date, COUNT(id) AS count FROM thearte_participants 
WHERE updated_at BETWEEN DATE_FORMAT(DATE_SUB(CURDATE(),INTERVAL 0 DAY), '%Y-%m-%d 00:00:00') AND DATE_FORMAT(DATE_SUB(CURDATE(),INTERVAL 0 DAY), '%Y-%m-%d 23:59:59') " . $str);

        return view('admin.participant.index')->with(['chartData' => json_encode($models)]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * 异步获取列表数据
     *
     * @param Request $request
     *
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse
     */
    public function lists(Request $request)
    {
        if ($request->ajax()) {
            $offset = intval($request->get('start'));
            $paginate = Participant::select(['id', 'openid', 'nickname', 'username', 'sex', 'age', 'id_no', 'mobile', 'email', 'headimgurl', 'created_at'])->orderBy('id', 'desc')
                ->paginate(self::PAGE_SIZE, ['*'], 'page', intval($offset / self::PAGE_SIZE) + 1);

            $data = [];
            foreach ($paginate as $item) {
                $data[] = [
                    $item->id,
                    $item->openid,
                    urldecode($item->nickname),
                    $item->username,
                    $item->sex,
                    $item->age,
                    $item->id_no,
                    $item->mobile,
                    $item->email,
                    '<img src="'. $item->headimgurl .'" width="50" />',
                    $item->created_at->toDateTimeString()
                    // ,
                    // '<a type="button" class="btn btn-primary btn-xs"><i class="fa fa-eye"></i> 查看</a>',
                ];
            }

            $records = [
                'data' => $data,
                'draw' => intval($request->get('draw')),
                'recordsTotal' => $paginate->total(),
                'recordsFiltered' => $paginate->total(),
            ];

            return response()->json($records);
        } else {
            return redirect()->guest('admin/login');
        }
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
